<?php

namespace app\admin\controller;


use app\admin\model\GC;
use cmf\controller\AdminBaseController;
use think\facade\Db;

/**
 * Class DuizhaoController
 * @package app\admin\controller
 * @adminMenuRoot(
 *     'name'   =>'钢种牌号关系维护',
 *     'action' =>'index',
 *     'parent' =>'',
 *     'display'=> true,
 *     'order'  => 10000,
 *     'icon'   =>'',
 *     'remark' =>''
 * )
 */
class DuizhaoController extends AdminBaseController
{

    public function index()
    {
        //搜索部分
//        $key_status = input('get.key_status/d');

        $lists = GC::order('id desc');
//        if ($key_status) {
//            $lists = $lists->where('status', $key_status);
//        }
        $lists = $lists->where('delete_time', 0);
        $lists = $lists->paginate(['list_rows' => 100, 'query' => [
            //'key_status' => $key_status
        ]]);
        $companytypea_arr = array(1 => '自主', 2 => '来料',3=>'委外');
        $this->assign('companytypea_arr', $companytypea_arr);

        $user_arr = Db::name('user')
            ->column('user_nickname', 'id');

        $this->assign('user_arr', $user_arr);
        $steel_grade_id_arr = Db::name('steel_grade')
            ->column('name', 'id');
        $this->assign('steel_grade_id_arr', $steel_grade_id_arr);
        $grade_id_arr = Db::name('grade')
            ->column('g_name', 'id');
        $this->assign('grade_id_arr', $grade_id_arr);
        return $this->fetch('', ['lists' => $lists->toArray(), 'page' => $lists->render(), 'domain' => request()->domain() . '/upload/']);
    }
    /**
     *  @adminMenu(
     *     'name'   => '新增',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '新增',
     *     'param'  => ''
     * )
     * @return mixed
     */
    public function add()
    { $companytypea_arr=array(0=>array('id'=>1,'name'=>'自主'),1=>array('id'=>2,'name'=>'来料'), 2 => array('id' => 3, 'name' => '委外'));
        $this->assign('companytypea_arr', $companytypea_arr);

        $grade_id_arr = Db::query('select id,g_name from cmf_grade where delete_time =0');
        $this->assign('grade_id_arr', $grade_id_arr);
        $steel_grade_id_arr = Db::query('select id,name from cmf_steel_grade where delete_time =0');
        $this->assign('steel_grade_id_arr', $steel_grade_id_arr);
        return $this->fetch('', ['domain' => request()->domain() . '/upload/']);
    }
    /**
     *  @adminMenu(
     *     'name'   => '新增提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '新增提交',
     *     'param'  => ''
     * )
     * @return mixed
     */
    public function addPost()
    {
        $params = array_map(function ($v) {
            return is_array($v) ? implode(',', $v) : $v;
        }, input('post.'));

        if (!$params) {
            $this->error('非法参数');
        }
        $params['add_time'] = time();
        $params['update_time'] = time();
        $params['admin_id'] = cmf_get_current_admin_id();
//        GC::create($params);
        $id = Db::name('g_c')->insertGetId($params);
        $params['id'] = $id;
        cmf_install_log($params, 'g_c_log', 'add');
        $this->success('操作成功', 'index');
    }
    /**
     *  @adminMenu(
     *     'name'   => '修改',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '修改',
     *     'param'  => ''
     * )
     * @return mixed
     */
    public function edit()
    {
        $id = input('get.id/d');

        if (!is_numeric($id)) {
            $this->error('非法参数');
        }
        $companytypea_arr=array(0=>array('id'=>1,'name'=>'自主'),1=>array('id'=>2,'name'=>'来料'), 2 => array('id' => 3, 'name' => '委外'));
        $this->assign('companytypea_arr', $companytypea_arr);

        $res = GC::where('id', $id)->find();
        $grade_id_arr = Db::query('select id,g_name from cmf_grade where delete_time =0');
        $this->assign('grade_id_arr', $grade_id_arr);
        $steel_grade_id_arr = Db::query('select id,name from cmf_steel_grade where delete_time =0');
        $this->assign('steel_grade_id_arr', $steel_grade_id_arr);

        return $this->fetch('', ['vo' => $res, 'domain' => request()->domain() . '/upload/']);
    }
    /**
     *  @adminMenu(
     *     'name'   => '修改提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '修改提交',
     *     'param'  => ''
     * )
     * @return mixed
     */
    public function editPost()
    {
        $params = array_map(function ($v) {
            return is_array($v) ? implode(',', $v) : $v;
        }, input('post.'));

        if (!$params || empty($params['id'])) {
            $this->error('非法参数');
        }
        $params['update_time'] = time();
        $params['admin_id'] = cmf_get_current_admin_id();
        GC::update($params);
        cmf_install_log($params, 'g_c_log', 'update');
        $this->success('操作成功', 'index');
    }
    /**
     *  @adminMenu(
     *     'name'   => '删除',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '删除',
     *     'param'  => ''
     * )
     * @return mixed
     */
    public function del()
    {
        $id = input('get.id/d');

        if (!(is_numeric($id) && $id > 0)) {
            $this->error('非法参数');
        }

        GC::where('id', $id)->update(array('delete_time' => time(), 'admin_id' => cmf_get_current_admin_id()));
        $params = Db::name('g_c')->where('id', $id)->find();
        cmf_install_log($params, 'g_c_log', 'del');
        $this->success('删除成功');
    }

    public function field()
    {
        $id = input('get.id/d');
        $field = input('get.field');

        if (!(is_numeric($id) && $id > 0 && $field)) {
            $this->error('非法参数');
        }

        $info = GC::field($field)->where('id', $id)->find();

        $this->success('ok', '', $info[$field]);
    }
}